/* eslint-disable no-tabs */
/* eslint-disable no-mixed-spaces-and-tabs */
import $http from '../utils/http.js'
import router from '../routers/router.js'
import { Notification } from 'atelement'
import { uncompileStr } from '../utils/encryption.js'
const changeLoading = 'setting/changeLoading'
const addUser = 'users/addUser' // 新增用户
const updateUser = 'users/updateUser' // 修改用户
const getUserdata = 'users/usersData' // 获取用户信息
const getDate = 'users/getDate' // 将接口取值赋值到store
const getCurrent = 'user/getCurrent' // 设置当前页码
const getPageize = 'user/getPageize' // 设置页码大小e
const getValues = 'user/getValues' // 设置模糊搜索值
const stopLogin = 'user/stopLogin' // 禁用人员
const setFilters = 'user/setFilters' // 设置过滤条件
const getPersonal = 'user/getPersonal' // 获取单个信息
const setPersonal = 'user/setPersonal' // 设置单个信息
const addFlag = 'user/addFlag'
const updataFlag = 'user/updataFlag'
const detailedFlag = 'user/detailedFlag'
const is = 'user/is'
const getSync = 'user/getSync' // 同步信息数量(用户)
const setSync = 'user/setSync'
const getSyncmessage = 'user/getSyncmessage' // 同步信息(用户)
const setSyncmessage = 'user/setSyncmessage'
const getSynclog = 'user/getSynclog'
const setSynclog = 'user/setSynclog'
const delSync = 'user/delSync' // 删除同步信息(用户)
const syncOrder = 'user/syncOrder' // 同步信息排序(用户)
const syncFilters = 'user/syncFilters' // 同步信息筛选(用户)
const userSyncvalue = 'user/userSyncvalue'
// const getNodename = 'mechanism/getNodename' // 获取上级单位选项信息
// const setNodename = 'mechanism/setNodename' // 设置上级单位选项信息
const getNodeselect = 'user/mechanism/getNodeselect' // 筛选机构选项
const setNodeselect = 'user/mechanism/setNodeselect' // 设置筛选机构选项
// const setDepartmenttree = 'mechanism/setDepartmenttree' // 机构树
// const getFdepartment = 'mechanism/getFdepartment' // 获取机构树信息
// const setDepartmentkey = 'mechanism/setDepartmentkey' // 机构树key
export default {
  state: {
    num: 0,
    snycCount: '',
    addFlag: false,
    updataFlag: false,
    detailedFlag: true,
    is: false, // 辅助标志位
    loading: false, // 加载状态
    personal: { 'fdepartment_name': [] }, // 个人详细信息
    userDate: {
      'pagination': { // 分页
        'current': 1, // 当前页码，类型：number
        'pageSize': 20 // 单页面数据条数，类型：number
      },
      'search': { 'value': '' },
      'total': null,
      'sorter': {
        'field': 'aagency_name', // 排序的字段
        'order': 'descend' // or 'desc'升序还是降序
      },
      'filters': {
        'aperson_id': [] // 需要过滤的字段
      },
      'table': [{ 'auser_group_name': '' }] // 用户信息列表
    },
    syncLog: {
      'pagination': { // 分页
        'current': 1, // 当前页码，类型：number
        'pageSize': 20 // 单页面数据条数，类型：number
      },
      'search': { 'value': '' },
      'total': null,
      'filters': {
        'aperson_id': [] // 需要过滤的字段
      },
      'sorter': {
        'field': 'zr_time', // 排序的字段
        'order': 'descend' // or 'desc'升序还是降序
      },
      'dict': { 'type': 'add' },
      'table': [] // 用户信息列表
    },
    syncDate: {
      'sorter': {
        'field': 'zr_time', // 排序的字段
        'order': 'descend' // or 'desc'升序还是降序
      },
      'filters': {
        'aperson_id': [] // 需要过滤的字段
      },
      'dict': { 'type': 'add' },
      'addtable': [], // 用户信息列表,
      'uptable': [],
      'deltable': []
    },
    nodeSelect: [],
    departmentList: []
  },
  actions: {
    [delSync] ({ commit, state, dispatch }, data) {
      $http.option('/api/user/delsnyccount').then(({ status, msg } = {}) => {
        if (status === 'success') {
          dispatch('group/getOptions', 1)
	        dispatch(getSyncmessage, { 'type': 'add' })
	        dispatch(getSyncmessage, { 'type': 'update' })
	        dispatch(getSyncmessage, { 'type': 'delete' })
          Notification.success({
            title: '删除成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '删除失败'
          })
        }
      })
    },
    [getSynclog] ({ commit, state }, data) {
      let form = JSON.parse(JSON.stringify(state.syncLog))
      form.dict.type = data.type
      $http.option('/api/user/synclog', form).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(setSynclog, data)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getSyncmessage] ({ commit, state }, datas) {
      let form = JSON.parse(JSON.stringify(state.syncDate))
      form.dict.type = datas.type
      $http.option('/api/user/snycmessage', form).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(setSyncmessage, { 'type': datas, 'data': data })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getSync] ({ commit }) {
      $http.post('/api/user/snyccount').then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(setSync, msg)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getPersonal] ({ commit }, data) {
      $http.post('/api/user/personal', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          let data = JSON.parse(JSON.stringify(msg[0]))
          if (msg[0].user_group_id == null) {
            data.user_group_id = []
          } else {
            data.user_group_id = msg[0].user_group_id.split(',')
          }
          commit(setPersonal, data)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限',
            message: msg
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [stopLogin] ({ commit, dispatch }, data) {
      commit(changeLoading, true)
      $http.post('/api/user/stop', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(changeLoading, false)
          dispatch(getUserdata)
          Notification.success({
            title: '删除用户成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '删除用户失败'
          })
        }
      })
    },
    [getUserdata] ({ commit, state }) {
      const form = state.userDate
      commit(changeLoading, true)
      $http.option('api/user/message', form).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(changeLoading, false)
          commit(getDate, msg)
        } else if (status === 'notallow') {
          Notification.error({
            title: '没有操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [addUser] ({ commit, dispatch }, dataAll) {
      $http.post('api/user/add', dataAll).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(addFlag, false)
          commit(detailedFlag, true)
          dispatch(getUserdata)
          Notification.success({
            title: '添加用户成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '添加用户失败'
          })
        }
      })
    },
    [updateUser] ({ commit, dispatch }, dataAll) {
      $http.post('api/user/update', dataAll).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(updataFlag, false)
          commit(detailedFlag, true)
          dispatch(getUserdata)
          Notification.success({
            title: '修改信息成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '修改用户失败'
          })
        }
      })
    },
    [getNodeselect] ({ commit }) {
      // 新修改
      $http.post('api/user/node', { 'aagency_id': '1', 'type': 'user' }).then(({ status, msg } = {}) => {
        if (status === 'success') {
          let option = []
          for (let i = 0; i < msg.length; i++) {
            let nodeName = {}
            nodeName['value'] = msg[i].agency_name
            nodeName['text'] = msg[i].agency_name
            nodeName['key'] = msg[i].agency_id
            option.push(nodeName)
          }
          commit(setNodeselect, option)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取机构选项失败'
          })
        }
      })
    }
    // [getFdepartment] ({ commit }) {
    //   $http.get('/api/user/fdepartment').then(({ status, msg, key } = {}) => {
    //     if (status === 'success') {
    //       commit(setDepartmenttree, msg)
    //       var list = []
    //       for (var i = 0; i < key.length; i++) {
    //         list.push(key[i].node_id)
    //       }
    //       commit(setDepartmentkey, list)
    //     } else if (status === 'notallow') {
    //       Notification.error({
    //         title: '不具备权限'
    //       })
    //     } else if (status === 'fail') {
    //       Notification.error({
    //         title: '获取组织机构树信息失败'
    //       })
    //     }
    //   })
    // }
    // [getNodename] ({ commit }, data) {
    //   $http.post('api/mechanism/node', data).then(({ status, msg } = {}) => {
    //     if (status === 'success') {
    //       commit(setNodename, msg)
    //     } else if (status === 'notallow') {
    //       Notification.error({
    //         title: '不具备权限'
    //       })
    //     } else if (status === 'fail') {
    //       Notification.error({
    //         title: '获取机构选项失败'
    //       })
    //     }
    //   })
    // }
  },
  mutations: {
    [setSynclog] (state, value) {
      state.syncLog.table = value.dataSource
      state.syncLog.total = value.total
      state.syncLog.pagination.current = value.current
    },
    [setSyncmessage] (state, value) {
      if (value.type.type === 'add') {
        state.syncDate.addtable = value.data.dataSource
      } else if (value.type.type === 'update') {
        state.syncDate.uptable = value.data.dataSource
      } else {
        state.syncDate.deltable = value.data.dataSource
      }
    },
    [syncOrder] (state, value) {
      state.syncDate.sorter.field = value.prop
      state.syncDate.sorter.order = value.order
      state.syncLog.sorter.field = value.prop
      state.syncLog.sorter.order = value.order
    },
    [setSync] (state, value) {
      state.snycCount = value.count
    },
    [detailedFlag] (state, value) {
      state.detailedFlag = value
    },
    [updataFlag] (state, value) {
      state.updataFlag = value
    },
    [addFlag] (state, value) {
      state.addFlag = value
    },
    [getDate] (state, data) {
      state.userDate.pagination.current = data.current
      state.userDate.total = data.total
      state.userDate.table = uncompileStr(data.dataSource, ['email', 'mobile_phone', 'person_name'])
    },
    [getCurrent] (state, val) {
      state.userDate.pagination.current = val
      state.syncLog.pagination.current = val
    },
    [getPageize] (state, val) {
      state.userDate.pagination.pageSize = val
      state.syncLog.pagination.pageSize = val
    },
    [getValues] (state, value) {
      state.userDate.search.value = value
    },
    [userSyncvalue] (state, value) {
      state.syncLog.search.value = value
    },
    [setFilters] (state, value) {
      state.userDate.filters.aperson_id = value
    },
    [syncFilters] (state, value) {
      state.syncDate.filters.aperson_id = value
      state.syncLog.filters.aperson_id = value
    },
    [is] (state, value) {
      state.is = value
    },
    [setPersonal] (state, value) {
      state.personal = uncompileStr([value], ['email', 'mobile_phone', 'person_name'])[0]
    },
    [changeLoading] (state, value) {
      state.loading = value
    },
    [setNodeselect] (state, value) {
      state.nodeSelect = value
    }
    // [setDepartmenttree] (state, value) {
    //   state.departmentList = value
    // }
    // [setNodename] (state, value) {
    //   state.division = value
    // }
  }
}
